package com.jqb.jsplab.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 使用IP+时间+3位随机数重命名文件
 */
public class Rename {

	private SimpleDateFormat sdf;
	private String ip;
	public Rename(){
		
	}
	public Rename(String ip){
		this.ip=ip;
	}
	public String getNewName(){
		StringBuffer buf=new StringBuffer();
		/**
		 * 获取IP,去点,补零
		 */
		if(this.ip!=null){
			String s[]=this.ip.split("\\.");
			for(int i=0;i<s.length;i++){
				buf.append(this.addZero(s[i],3));
			}
		}
		/**
		 * 获取时间戳
		 */
		buf.append(this.getTime());
		/**
		 * 添加三位随机数
		 */
		Random r=new Random();
		for(int i=0;i<3;i++){
			buf.append(r.nextInt(10));
		}
		return buf.toString();
	}
	
	public String getDate(){
		this.sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		return this.sdf.format(new Date());
	}
	
	public String getTime(){
		this.sdf=new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return this.sdf.format(new Date());
	}
	
	public String addZero(String s,int len){
		StringBuffer str=new StringBuffer();
		str.append(s);
		while(str.length()<len){
			str.insert(0, "0");
		}
		return str.toString();
	}
	
	
	public static void main(String[] args) {
		Rename re =new Rename("192.168.1.1");
		System.out.println(re.getNewName());
	}

}
